package com.bsh.serviceImpl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.bsh.repository.DaoAccountState;
import com.bsh.service.AccountStateService;
import com.bsh.utils.DecimalFormatTool;
import com.bsh.utils.KeyValuePair;

/**
 * 资金统计
 * 
 * @author gsh
 *
 */
@Service
@Transactional
public class AccountStateServiceImpl implements AccountStateService {
	@Autowired
	DaoAccountState daoState;

	/**
	 * 当前系统合计
	 * 
	 * @return
	 */
	@Override
	public Map<String,Object> getSysStates() {
		//--待处理--
		Map<String,Object> res = new HashMap<String,Object>();
		res.put("MONEY", String.format("%s", DecimalFormatTool.commaFormat(daoState.getMoneySum())));	
		res.put("CLOUD_MONEY", String.format("%s", DecimalFormatTool.commaFormat(daoState.getCloudSum())));		
		return res;		
	}
	
	/**
	 * 统计
	 * 
	 * @return
	 */
	@Override
	public List<KeyValuePair<String, String>> getStates(String beginDate, String endDate) {
		List<KeyValuePair<String, String>> res = new ArrayList<KeyValuePair<String, String>>();

		String endDt = null;
		if (!StringUtils.isEmpty(endDate)) {
			Date dt=new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				dt = sdf.parse(endDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}

			Calendar cal2 = Calendar.getInstance();
			cal2.setTime(dt);
			cal2.add(Calendar.DAY_OF_MONTH, 1);
			endDt= sdf.format(cal2.getTime());
		}

		StringBuilder sb = new StringBuilder();
		if (!StringUtils.isEmpty(beginDate) && StringUtils.isEmpty(endDt)) {
			sb.append(String.format(" and create_time >='%s' ", beginDate.trim()));
		} else if (StringUtils.isEmpty(beginDate) && !StringUtils.isEmpty(endDt)) {
			sb.append(String.format(" and create_time <='%s' ", endDt.trim()));
		} else if (!StringUtils.isEmpty(beginDate) && !StringUtils.isEmpty(beginDate)) {
			sb.append(String.format(" and (create_time between '%s' and '%s') ", beginDate.trim(), endDt.trim()));
		}
		String where = sb.toString();

		BigDecimal[] ms = daoState.getRechargeOnline(where);
		res.add(new KeyValuePair<String, String>("在线充值(元)",
				String.format("¥%s = ¥%s(支付宝) + ¥%s(微信)", ms[0].add(ms[1]), ms[0], ms[1])));
		res.add(new KeyValuePair<String, String>("提现(元)", String.format("¥%s", daoState.getCash(where))));
		if (false)
			res.add(new KeyValuePair<String, String>("余额转账转入(元)", String.format("¥%s", daoState.getTranferIn(where))));
		if (false)
			res.add(new KeyValuePair<String, String>("余额转账转出(元)", String.format("¥%s", daoState.getTranferOut(where))));
		if (false)
			res.add(new KeyValuePair<String, String>("在线转账转入(元)",
					String.format("¥%s", daoState.getTranferInOnline(where))));
		if (false)
			res.add(new KeyValuePair<String, String>("在线转账转出(元)",
					String.format("¥%s", daoState.getTranferOutOnline(where))));
		res.add(new KeyValuePair<String, String>("昨日返现(元)", String.format("¥%s", daoState.getRefundMony(where))));
		ms = daoState.getReferralRefund(where);
		
		if(false)res.add(new KeyValuePair<String, String>("推荐人返利(元)",
				String.format("¥%s = ¥%s(支付宝) + ¥%s(微信)", ms[0].add(ms[1]), ms[0], ms[1])));
		res.add(new KeyValuePair<String, String>("余额升级VIP(元)",
				String.format("¥%s", daoState.getUpgradeVipByBalance(where))));
		ms = daoState.getUpgradeVipOnline(where);
		res.add(new KeyValuePair<String, String>("在线升级VIP(元)",
				String.format("¥%s = ¥%s(支付宝) + ¥%s(微信)", ms[0].add(ms[1]), ms[0], ms[1])));
		res.add(new KeyValuePair<String, String>("余额升级金钻(元)",
				String.format("¥%s", daoState.getUpgradeGoldByBalance(where))));
		ms = daoState.getUpgradeGoldOnline(where);
		res.add(new KeyValuePair<String, String>("在线升级金钻(元)",
				String.format("¥%s = ¥%s(支付宝) + ¥%s(微信)", ms[0].add(ms[1]), ms[0], ms[1])));
		res.add(new KeyValuePair<String, String>("后台充值(现金)", String.format("¥%s", daoState.getRechargeByAdmin(where))));
		res.add(new KeyValuePair<String, String>("后台扣除(现金)", String.format("¥%s", daoState.getDeductionByAdmin(where))));
		
		//--待处理--
		res.add(new KeyValuePair<String, String>("后台充值(商币)", String.format("¥%s", daoState.getRechargeCloudByAdmin(where))));
		res.add(new KeyValuePair<String, String>("后台扣除(商币)", String.format("¥%s", daoState.getDeductionCloudByAdmin(where))));

		
		return res;
	}
}
